from optparse import OptionParser
from Highlighter import Highlighter
from OutputBuilder import HtmlOutputBuilder
from Storage import LanguagesStorage, StylesStorage

__author__ = 'deadmoroz'


def main():
    (inputPath, outputPath, language, style) = parse_option()

    inputBody = open(inputPath).read()
    builder = HtmlOutputBuilder(style)
    Highlighter().highlight(language, inputBody, builder)

    outputFile = open(outputPath, "w")
    outputFile.write(builder.get_content())


def get_epilog():
    epilog = []
    languages_storage = LanguagesStorage()
    epilog.append("Supported LANGUAGEs:")
    for language_name in languages_storage.get_supported_languages():
        epilog.append(language_name)
    epilog.append("Supported STYLEs:")
    styles_storage = StylesStorage()
    for style_name in styles_storage.get_supported_styles():
        epilog.append(style_name)
    return "\r\n".join(epilog)


def parse_option():
    parser = OptionParser(epilog=get_epilog())
    parser.add_option("-i", "--input", dest="input",
        help="FILE to highlight")
    parser.add_option("-o", "--output", dest="output",
        help="FILE for highlight result")
    parser.add_option("-s", "--style", dest="style",
        help="STYLE of highlight")
    parser.add_option("-l", "--language", dest="language",
        help="LANGUAGE of FILE to highlight")

    (options, args) = parser.parse_args()
    if options.input and options.output and options.language and options.language:
        return (options.input, options.output, options.language, options.style)
    parser.error("all options required")

main()